package org.example;

import com.alibaba.fastjson.JSONObject;
import com.wxh.ks.handler.KsHandler;
import javassist.ClassClassPath;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;

import java.util.Date;

public class UpdateMethod {

    private static String pathName = "D:\\pj";
    private static String className = "com.wxh.ks.handler.KsHandler";

    public static void main(String[] args) {
        updateMethod();
    }

    public static void updateMethod() {
        try {
            ClassPool cPool = new ClassPool(true);
            //如果该文件引入了其它类，需要利用类似如下方式声明
            //cPool.importPackage("java.util.List");

            //设置class文件的位置
            cPool.insertClassPath(new ClassClassPath(KsHandler.class));

            // 导入需要引入的 包
            cPool.importPackage("cn.hutool.core.io.IoUtil");
            cPool.importPackage("cn.hutool.core.lang.Filter");
            cPool.importPackage("cn.hutool.core.net.NetUtil");
            cPool.importPackage("cn.hutool.core.util.StrUtil");
            cPool.importPackage("cn.hutool.system.oshi.OshiUtil");
            cPool.importPackage("com.alibaba.fastjson.JSON");
            cPool.importPackage("com.alibaba.fastjson.JSONObject");
            cPool.importPackage("com.intellij.uiDesigner.core.GridConstraints");
            cPool.importPackage("com.wxh.ks.exception.CustomException");
            cPool.importPackage("com.wxh.ks.handler.AccountItem");
            cPool.importPackage("com.wxh.ks.handler.SucaiItem");
            cPool.importPackage("com.wxh.ks.swing.KsLogin");
            cPool.importPackage("com.wxh.ks.swing.KsMain");
            cPool.importPackage("com.wxh.ks.utils.AESEncryption");
            cPool.importPackage("com.wxh.ks.utils.CryptoUtils");
            cPool.importPackage("com.wxh.ks.utils.HttpClientUtils");
            cPool.importPackage("com.wxh.ks.utils.MyConstants");
            cPool.importPackage("com.wxh.ks.utils.SwingUtils");
            cPool.importPackage("com.wxh.ks.utils.TimeUtils");
            cPool.importPackage("java.io.File");
            cPool.importPackage("java.io.FileInputStream");
            cPool.importPackage("java.io.FileNotFoundException");
            cPool.importPackage("java.io.FileOutputStream");
            cPool.importPackage("java.io.IOException");
            cPool.importPackage("java.io.InputStream");
            cPool.importPackage("java.io.OutputStream");
            cPool.importPackage("java.lang.invoke.LambdaMetafactory");
            cPool.importPackage("java.net.Inet4Address");
            cPool.importPackage("java.net.InetAddress");
            cPool.importPackage("java.net.URLEncoder");
            cPool.importPackage("java.nio.charset.Charset");
            cPool.importPackage("java.nio.charset.StandardCharsets");
            cPool.importPackage("java.text.SimpleDateFormat");
            cPool.importPackage("java.util.ArrayList");
            cPool.importPackage("java.util.Arrays");
            cPool.importPackage("java.util.Date");
            cPool.importPackage("java.util.HashMap");
            cPool.importPackage("java.util.HashSet");
            cPool.importPackage("java.util.Iterator");
            cPool.importPackage("java.util.List");
            cPool.importPackage("java.util.Map");
            cPool.importPackage("java.util.Set");
            cPool.importPackage("java.util.concurrent.ConcurrentHashMap");
            cPool.importPackage("java.util.concurrent.TimeUnit");
            cPool.importPackage("java.util.concurrent.atomic.AtomicInteger");
            cPool.importPackage("java.util.function.Function");
            cPool.importPackage("javax.crypto.Cipher");
            cPool.importPackage("javax.swing.table.DefaultTableModel");
            cPool.importPackage("org.apache.poi.ss.usermodel.Row");
            cPool.importPackage("org.openqa.selenium.By");
            cPool.importPackage("org.openqa.selenium.Cookie");
            cPool.importPackage("org.openqa.selenium.WebDriver");
            cPool.importPackage("org.openqa.selenium.WebElement");
            cPool.importPackage("org.openqa.selenium.chrome.ChromeDriver");
            cPool.importPackage("org.openqa.selenium.support.ui.ExpectedConditions");
            cPool.importPackage("org.openqa.selenium.support.ui.WebDriverWait");


            //获取该class对象
            CtClass cClass = cPool.get(className);

            //获取到对应的方法
            CtMethod[] cMethods = cClass.getDeclaredMethods("a");
            CtMethod aMethod = null;
            for (CtMethod cMethod : cMethods) {
                if (cMethod.getParameterTypes() != null && cMethod.getParameterTypes().length == 3) {
                    if (cMethod.getParameterTypes()[0].getName().contains("String")
                            && cMethod.getParameterTypes()[1].getName().contains("String")
                            && cMethod.getParameterTypes()[2].getName().contains("Integer")) {
                        aMethod = cMethod;
                    }
                }
            }
            System.out.println(aMethod.getLongName());

            //更改该方法的内部实现
            //需要注意的是对于参数的引用要以$开始，不能直接输入参数名称
            aMethod.setBody("{return true;}");


            CtMethod checkCdKeys = cClass.getDeclaredMethod("checkCdKey");

            checkCdKeys.setBody("{\n" +
                    "        JSONObject jsonObject = new JSONObject();\n" +
                    "        jsonObject.put(\"maxNum\", \"1000000\");\n" +
                    "        jsonObject.put(\"createTime\", new Date());\n" +
                    "        jsonObject.put(\"cdkey\", \"9053eb4b53974e1f8630e35bc7e43ac0\");\n" +
                    "        jsonObject.put(\"bindNum\", \"111\");\n" +
                    "        jsonObject.put(\"expireTime\", \"20290808\");\n" +
                    "        java.io.FileOutputStream a11 = com.wxh.ks.swing.KsLogin.logOutPut;\n" +
                    "        Object[] a12 = new Object[1];\n" +
                    "        a12[0] = new StringBuilder().append(\"激活码校验成功，到期时间：\").append(jsonObject.get(\"expireTime\")).append(\"\\n\").toString();\n" +
                    "        cn.hutool.core.io.IoUtil.writeUtf8((java.io.OutputStream)a11, false, a12);\n" +
                    "        return jsonObject;\n" +
                    "    }");

            //替换原有的文件
            cClass.writeFile(pathName);

            System.out.println("=======change finish=========");

        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public JSONObject dd() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("maxNum", "1000000");
        jsonObject.put("createTime", new Date());
        jsonObject.put("cdkey", "9053eb4b53974e1f8630e35bc7e43ac0");
        jsonObject.put("bindNum", "111");
        jsonObject.put("expireTime", "20290808");
        java.io.FileOutputStream a11 = com.wxh.ks.swing.KsLogin.logOutPut;
        Object[] a12 = new Object[1];
        a12[0] = new StringBuilder().append("激活码校验成功，到期时间：").append(jsonObject.get("expireTime")).append("\n").toString();
        cn.hutool.core.io.IoUtil.writeUtf8((java.io.OutputStream) a11, false, a12);
        return jsonObject;
    }

}
